package org.sojex.photo.util;

import org.sojex.R;
import org.sojex.photo.bean.CookBean;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	// private ArrayList<CookBean> items;
	private int pos;
	Context context;

	public GAdapter(Context context, int pos) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.pos = pos;
	}

	public int getCount() {
		return GlobalData.cookBook.get(pos).cooks.size();
	}

	public CookBean getItem(int position) {
		return GlobalData.cookBook.get(pos).cooks.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.photo_gallery_row, parent,
					false);

			holder = new ViewHolder();
			holder.image = (ImageView) convertView
					.findViewById(R.id.imageView1);
			holder.text = (TextView) convertView.findViewById(R.id.textView1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		CookBean bean = GlobalData.cookBook.get(pos).cooks.get(position);
		holder.text.setText(bean.name);
		if (bean.bitmap != null) {
			holder.image.setImageBitmap(bean.bitmap);
		} else {
			if(bean.uri!=null&&Util.isExist(bean.uri))
				holder.image.setImageDrawable(Drawable.createFromPath(bean.uri));
			else
				holder.image.setImageResource(R.drawable.pic);
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView image;// 图片
		TextView text;
	}
}
